package com.flyme.module.order.provider.service.impl;


import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.order.client.entity.Order;
import com.flyme.module.order.client.entity.PayAccount;
import com.flyme.module.order.provider.mapper.PayAccountMapper;
import com.flyme.module.order.provider.service.PayAccountService;
import com.flyme.module.order.provider.service.PayResponse;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 支付账户管理接口实现
 */
@Service
public class PayAccountServiceImpl extends BaseServiceImpl<PayAccountMapper, PayAccount> implements PayAccountService {
    /**
     * 缓存
     */
    private final static Map<String, PayResponse> payResponses = new HashMap<>();
    private final static Map<String, PayAccount> payconfigs = new HashMap<>();
    @Resource
    private AutowireCapableBeanFactory spring;

    /**
     * 获取支付响应
     */
    public PayResponse getPayResponse(HttpServletRequest request) {
        // 账户id
        String payCode = request.getParameter("payCode");
        PayAccount payAccount = payconfigs.get(payCode);
        PayResponse payResponse = getPayResponse(payAccount, payCode);
        if(ObjectUtils.isEmpty(payAccount)){
            payAccount = payconfigs.get(payCode);
        }
        payResponse.init(request, payAccount);
        return payResponse;
    }

    /**
     * 获取支付响应
     */
    public PayResponse getPayResponsePC(HttpServletRequest request) {
        // 账户id
        String payCode = request.getParameter("payCode");
        PayAccount payAccount = payconfigs.get(payCode);
        PayResponse payResponse = getPayResponse(payAccount, payCode);
        if(ObjectUtils.isEmpty(payAccount)){
            payAccount = payconfigs.get(payCode);
        }
        payResponse.initPC(request, payAccount);
        return payResponse;
    }

    /**
     * 获取响应
     */
    public PayResponse getPayCallBackResponse(HttpServletRequest request, Order order) {
        // 账户id
        String payCode = order.getPayType().getCode();
        PayAccount payAccount = payconfigs.get(payCode);
        PayResponse payResponse = getPayResponse(payAccount, payCode);
        payAccount.setOrder(order);
        payResponse.setCallBack(request, payAccount);
        return payResponse;
    }


    private PayResponse getPayResponse(PayAccount payAccount, String payCode) {
        PayResponse payResponse = payResponses.get(payCode);
        if (payResponse == null) {
            payAccount = getOne(q().eq("payCode", payCode));
            if (payAccount == null) {
                throw new IllegalArgumentException("支付账户配置不存在");
            }
            payResponse = new PayResponse();
            spring.autowireBean(payResponse);
            payResponses.put(payCode, payResponse);
            payconfigs.put(payCode, payAccount);
        }
        return payResponse;
    }

    /**
     * 根据支付类型查询
     */
    public PayAccount getByPayType(String payType) {
        return getOne(q().eq("payCode", payType));
    }

}
